#region Imported Namespaces
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

using BO = PHSRAG.Insight21.BusinessObjects;
using Utility = PHSRAG.Utility;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using DataAccess = PHSRAG.Utility.DataAccess;
#endregion

namespace PHSRAG.Insight21.COI
{
	/// <summary>
	/// COIEMailTemplate
	/// </summary>
	public class COIEMailTemplate : BO.SmartBusinessObject
	{
		#region Constants
		private const string	coiEMailKey = "COIEMail_2ADFA5AC-2000-4d0f-A88C-48D1BB908990";
		private const string ColumnId = "Id";
		private const string ColumnType = "Type";
		private const string ColumnBodyText = "BodyText";
		private const string ColumnSubject = "Subject";
		#endregion

		#region Instance Variables
		private string type;
		private string bodyText;
		private string subject = string.Empty;
		#endregion

		#region Statics
		/// <summary>
		/// Key used to place and/or retrieve this object from the cache.
		/// </summary>
		public static string COIEMailKey
		{
			get { return coiEMailKey; }
		}
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the type of the mail template
		/// </summary>
		public string Type
		{
			get { return type; }
			set { SetDirty(ref type, value); }
		}


		/// <summary>
		/// Gets or sets the text for the mail template
		/// </summary>
		public string BodyText
		{
			get { return bodyText; }
			set { SetDirty(ref bodyText, value); }
		}

		/// <summary>
		/// Gets or sets the text for the mail template
		/// </summary>
		public string Subject
		{
			get { return subject; }
			set { SetDirty(ref subject, value); }
		}
		#endregion

		#region Constructor
		/// <summary>
		/// Initilize the base class with the specified cache policy.
		/// </summary>
		/// <param name="cachePolicy">Prevalent cache policy</param>
		public COIEMailTemplate(ICachePolicy cachePolicy) : base(cachePolicy)
		{
		
		}
		#endregion

		#region SmartBusinessObject methods
		/// <summary>
		/// Cache Key uniquely identifies this object within the cache.
		/// </summary>
		/// <returns>the unique string for this object.</returns>
		public override string CacheKey()
		{
			return coiEMailKey;
		}
		
		
		/// <summary>
		/// Cache this instance using the prevalent cache policy.
		/// </summary>
		public override void Cache()
		{
			CachePolicy.Cache(coiEMailKey, this);
		}

		
		/// <summary>
		/// Remove this instance from the cache using the prevalent cache policy.
		/// </summary>
		public override void UnCache()
		{
			CachePolicy.UnCache(coiEMailKey);
		}
		
		
		/// <summary>
		/// Loads the COIEMailTemplate object with the specified id.
		/// </summary>
		/// <param name="connectionString">Information to connect to the database</param>
		/// <param name="args">COI Id of the document to be loaded</param>
		public override void Load(string connectionString, params object[] args)
		{
			if ( args.Length != 1 )
				throw new Exception("COIEMailTemplate Load expects type");

			try
			{
				DataSet mailTemplateDetails = Utility.DataAccess.GetDataSet(connectionString, "GetEMailTemplate", 
					Utility.DataAccess.BuildSqlParameterArray("@type", SqlDbType.VarChar, (string)args[0],
					"@moduleType", SqlDbType.VarChar, PHSRAG.Insight21.BusinessObjects.ModuleKey.COI) );
				
				DataRowCollection rows = mailTemplateDetails.Tables[0].Rows;
				if (rows.Count > 0)
					PopulateMembers(rows[0]);
			}
			catch( Exception e)
			{
				throw new Exception("Failed to load the COIEMailTemplate", e);
			}
		}
		
		
		/// <summary>
		/// Saves the state of the COIEmailTemplate object to the database. 
		/// The stored procedure inserts a new record if the id of the object is 0, 
		/// otherwise the procedure updates the record with the matching email template id
		/// </summary>
		/// <param name="sqlTransaction">The context in which the insert/update should be executed</param>
		protected override void SaveObject(System.Data.SqlClient.SqlTransaction sqlTransaction)
		{
			try
			{
				SqlParameter[] paramArray = DataAccess.BuildSqlParameterArray(
					"@id", SqlDbType.Int, Id, 
					"@type", SqlDbType.VarChar, type,
					"@bodyText", SqlDbType.NText, (bodyText == string.Empty) ? DBNull.Value : (object)bodyText,
					"@moduleType", SqlDbType.VarChar, PHSRAG.Insight21.BusinessObjects.ModuleKey.COI,
					"@subject",SqlDbType.VarChar,(subject == string.Empty) ? DBNull.Value : (object)subject);
			
				Id = Convert.ToInt32 (DataAccess.ExecuteScalar( sqlTransaction.Connection, sqlTransaction, "SaveEMailTemplate", 
					paramArray));
				
			}
			catch( Exception e)
			{
				throw new Exception("Failed to save the COIEMailTemplate", e);
			}
		}

		
		/// <summary>
		/// Deletes the mail template from the database.
		/// </summary>
		/// <param name="sqlTransaction">The transaction context in which the SQL statement is to be executed</param>
		protected override void DeleteObject(System.Data.SqlClient.SqlTransaction sqlTransaction)
		{
			throw new Exception("This method is not yet implemented");
		}

		#endregion


		#region Private methods
		/// <summary>
		/// Populates instance fields with corresponding values in the datarow
		/// </summary>
		/// <param name="dataRow">DataRow holding the values to be assigned to instance variables</param>
		private void PopulateMembers(DataRow dataRow)
		{
			Id = (int)dataRow[ColumnId];
			type = (string)dataRow[ColumnType];
			
			Object dbValue = dataRow[ColumnBodyText];
			bodyText = (dbValue == DBNull.Value) ? string.Empty : (string)dbValue;

			dbValue = dataRow[ColumnSubject];
			subject = (dbValue == DBNull.Value) ? string.Empty : (string)dbValue;

		}
		#endregion

	}
}
